<template>
  <div class="reBuildBtn-wrapper">
    <!-- 重新生成 modal-->
    <basic-modal ref="reBuildModal" static="true" width="450">
      <h3 slot="title">重新生成</h3>
      <div class="modal-body" slot="modal-body">
        <el-form
          ref="reBuildForm"
          label-position="left"
          label-width="70px"
          :model="reBuildFormData"
          :rules="reBuildRules"
        >
          <el-form-item label="停车场" prop="parkId" class="parkId-item">
            <el-select v-model="reBuildFormData.parkId" placeholder="请选择" multiple collapse-tags>
              <el-option v-for="item in parkList" :label="item.name" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="时间维度" prop="type">
            <el-select
              v-model="reBuildFormData.type"
              placeholder="请选择"
              @change="changeTimeDimension"
            >
              <el-option v-for="item in timeDimensionList" :value="item.value" :label="item.name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="生成时间" prop="date">
            <el-date-picker
              v-model="reBuildFormData.date"
              :type="createTime.type"
              :value="createTime.value"
              :format="createTime.format"
              :picker-options="createTime.pickerOptions"
              placeholder="选择日期"
            ></el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <div class="modal-footer" slot="modal-footer">
        <button class="ake_btn ake_btn_text ake_btn_confirm" @click="confirmOperate()">确定</button>
        <button class="ake_btn ake_btn_text modal-close" @click="closed('reBuildModal')">关闭</button>
      </div>
    </basic-modal>
  </div>
</template>

<script>
//modal
import invokeModal from "@mixins/modal/invokeModal.js";
import utils from "@mixins/utils.js";
export default {
  name: "reBuildBtn",
  mixins: [invokeModal, utils],
  props: {
    reqUrl: {
      type: String,
      required: true
    },
    timeDimensionList: {
      type: Array,
      required: true
    }
  },
  data() {
    var rebuild_today = new Date(
      moment()
        .subtract(1, "days")
        .format("YYYY-MM-DD")
    );
    var rebuild_month = new Date(
      moment()
        .subtract(1, "month")
        .format("YYYY-MM")
    );
    return {
      pageData: null,
      reBuildFormData: {
        parkId: [], //车场id-重新生成
        type: this.timeDimensionList[0].value, //时间维度-重新生成(date/month)
        date:
          this.timeDimensionList[0].value == "date"
            ? rebuild_today
            : rebuild_month //生成时间-重新生成
      },
      createTime: {
        type: this.timeDimensionList[0].value,
        value:
          this.timeDimensionList[0].value == "date"
            ? rebuild_today
            : rebuild_month,
        format:
          this.timeDimensionList[0].value == "date" ? "yyyy-MM-dd" : "yyyy-MM",
        pickerOptions: {
          disabledDate: this.disabledDateForDay
        }
      },
      reBuildRules: {
        parkId: [
          {
            required: true,
            message: "请选择停车场",
            trigger: "change"
          }
        ],
        type: [
          {
            required: true,
            message: "请选择时间维度",
            trigger: "change"
          }
        ],
        date: [
          {
            required: true,
            message: "请选择时间",
            trigger: "change"
          }
        ]
      },
      parkList: []
    };
  },
  created() {
    this.getAllParkingLot();
  },
  methods: {
    disabledDateForDay(time) {
      return time.getTime() > Date.now() - 8.64e7;
    },
    // 重新生成-确认提交
    confirmOperate() {
      var self = this;
      this.$refs["reBuildForm"].validate(valid => {
        if (valid) {
          let params = {
            parkId: self.reBuildFormData.parkId.join(","),
            type: self.reBuildFormData.type
          };
          if (self.reBuildFormData.type == "date") {
            params.date = moment(self.reBuildFormData.date).format(
              "YYYY-MM-DD"
            );
          }
          if (self.reBuildFormData.type == "month") {
            params.date = moment(self.reBuildFormData.date).format("YYYY-MM");
          }
          if (self.reBuildFormData.type == "year") {
            params.date = moment(self.reBuildFormData.date).format("YYYY");
          }
          API.fetchGet(this.reqUrl, params).then(
            res => {
              if (res.status == 1 && !res.message) {
                self.showToast("报表重新生成中......", 1, 1500);
              } else {
                self.showToast(res.message, 1, 1500);
              }
              self.closed("reBuildModal");
            },
            err => {}
          );
        } else {
          return false;
        }
      });
    },
    // 获取所有停车场
    getAllParkingLot() {
      API.fetchGet("/commonFun/queryAllPark.do", {}).then(
        res => {
          let data = res.data;
          let list = data && data.parkList ? data.parkList : [];

          this.parkList = [];
          list &&
            list.forEach(item => {
              let obj = {
                name: item.name,
                value: item.id
              };
              this.parkList.push(obj);
            });
        },
        err => {}
      );
    },
    // 重新生成按钮
    reBuild() {
      this.toOpenModal("reBuildModal");
    },
    changeTimeDimension(value) {
      var today = new Date(
        moment()
          .subtract("days", 1)
          .format("YYYY-MM-DD")
      );
      var month = new Date(
        moment()
          .subtract("month", 1)
          .format("YYYY-MM")
      );
      var year = new Date(
        moment()
          .subtract("year")
          .format("YYYY")
      );
      switch (value) {
        case "date":
          this.createTime.type = "date";
          this.createTime.format = "yyyy-MM-dd";
          this.createTime.value = today;
          this.reBuildFormData.date = today;
          break;
        case "month":
          this.createTime.type = "month";
          this.createTime.format = "yyyy-MM";
          this.createTime.value = month;
          this.reBuildFormData.date = month;
          break;
        case "year":
          this.createTime.type = "year";
          this.createTime.format = "yyyy";
          this.createTime.value = year;
          this.reBuildFormData.date = year;
          break;
      }
    }
  }
};
</script>

<style lang="less">
.reBuildBtn-wrapper {
  .el-input__inner {
    height: 40px;
    line-height: 40px;
    width: 100%;
  }
  .parkId-item {
    .el-form-item__content {
      .el-select__tags {
        max-width: unset !important;
      }
    }
  }
}
</style>

